<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html;charset=utf-8" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
  <title>设置权限</title>
  <link rel="icon" href="favicon.ico" type="image/ico">
  <link href="${base}/css/bootstrap.min.css" rel="stylesheet">
  <link href="${base}/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="${base}/css/style.min.css" rel="stylesheet">
</head>

<body>

<div class="container-fluid">

  <div class="row">
    <div class="col-lg-12">
      <div class="card">
        <div class="card-header"><h4>设置权限</h4></div>
        <div class="card-body">

          <form action="${base}/role/saveMenu" method="post">
            <div class="form-group">
              <label for="example-text-input">角色名称</label>
              <input class="form-control" type="text" name="role-input" placeholder="角色名称"
                     value="${r.name}">
              <input type="hidden" name="roleid" value="${r.id}">
            </div>
            <div class="table-responsive">
              <table class="table table-striped">
                <thead>
                <tr>
                  <th>
                    <label class="lyear-checkbox checkbox-primary">
                      <input name="checkbox" type="checkbox" id="check-all">
                      <span> 全选</span>
                    </label>
                  </th>
                </tr>
                </thead>
                <tbody>
                <c:forEach var="one" items="${menus}">
                  <c:if test="${one.parentid eq -1}">
                    <tr>
                      <td>
                        <label class="lyear-checkbox checkbox-primary">
                          <input name="menuIds" type="checkbox" class="checkbox-parent" dataid="id-${one.id}"
                                 value="${one.id}">
                          <span>${one.name}</span>
                        </label>
                      </td>
                    </tr>
                    <c:forEach var="two" items="${menus}">
                      <c:if test="${two.parentid eq one.id}">
                        <tr>
                          <td class="p-l-40">
                            <label class="lyear-checkbox checkbox-primary">
                              <input name="menuIds" type="checkbox" class="checkbox-parent checkbox-child"
                                     dataid="id-${one.id}-${two.id}" value="${two.id}">
                              <span>${two.name}</span>
                            </label>
                          </td>
                        </tr>
                        <tr>
                          <td class="p-l-80">
                            <c:forEach var="three" items="${menus}">
                              <c:if test="${three.parentid eq two.id}">
                                <label class="lyear-checkbox checkbox-primary checkbox-inline">
                                  <input name="menuIds" type="checkbox" class="checkbox-child"
                                         dataid="id-${one.id}-${two.id}-${three.id}" value="${three.id}">
                                  <span>${three.name}</span>
                                </label>
                              </c:if>
                            </c:forEach>
                          </td>
                        </tr>
                      </c:if>
                    </c:forEach>
                  </c:if>
                </c:forEach>
                </tbody>
              </table>
            </div>
            <div class="form-group col-md-12">
              <button type="submit" class="btn btn-primary ajax-post" target-form="add-form">确 定</button>
              <button type="button" class="btn btn-default" onclick="javascript:history.back(-1);return false;">返 回
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

  </div>

</div>

<script type="text/javascript" src="${base}/js/jquery.min.js"></script>
<script type="text/javascript" src="${base}/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${base}/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="${base}/js/main.min.js"></script>

<script type="text/javascript" src="${base}/js/chosen.jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        var trs = document.getElementsByTagName("tr");
        for (let i = 0; i < trs.length; i++) {
            var v = trs[i].innerText;
            if (v == "") trs[i].style.display = "none";
        }
        <%--var rs = int[${r.menus.size()}];--%>
        <%--for (let i = 0; i < ${r.menus.size()}; i++) {--%>
        <%--    rs[i]=${r.menus}[i].id;--%>
        <%--}--%>
        <%--var menus=document.getElementsByName("menuIds");--%>
        <%--for (let i = 0; i < menus.length; i++) {--%>
        <%--    menus[i].checked;--%>
        <%--}--%>
        //动态选择框，上下级选中状态变化
        $('input.checkbox-parent').on('change', function () {
            var dataid = $(this).attr("dataid");
            $('input[dataid^=' + dataid + '-]').prop('checked', $(this).is(':checked'));
        });
        $('input.checkbox-child').on('change', function () {
            var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
            var parent = $('input[dataid=' + dataid + ']');
            if ($(this).is(':checked')) {
                parent.prop('checked', true);
                //循环到顶级
                while (dataid.lastIndexOf("-") != 2) {
                    dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                    parent = $('input[dataid=' + dataid + ']');
                    parent.prop('checked', true);
                }
            } else {
                //父级
                if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                    parent.prop('checked', false);
                    //循环到顶级
                    while (dataid.lastIndexOf("-") != 2) {
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                            parent.prop('checked', false);
                        }
                    }
                }
            }
        });
    });
</script>
</body>
</html>